﻿using Castle.DynamicProxy;
using System;

namespace CastleDynamicProxyTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Castle.DynamicProxy!");

            //IInterceptor();
            //IAsyncInterceptor();
            //WorkAsyncInterceptorWarpper();
            WorkProxyGenerationHook();

            Console.ReadLine();
        }

        static void IInterceptor()
        {
            Console.WriteLine("IInterceptor");
            IWorker target = new Worker();
            ProxyGenerator generator = new ProxyGenerator();
            var interceptor = new WorkInterceptor();
            var worker = generator.CreateInterfaceProxyWithTarget(target, interceptor);
            worker.Start();
            worker.Excute();
            worker.Stop();
        }

        static void WorkAsyncInterceptorWarpper()
        {
            Console.WriteLine("WorkAsyncInterceptorWarpper 包装 IAsyncInterceptor");
            IWorker target = new Worker();
            var interceptor = new WorkAsyncInterceptor();
            var wrapperInterceptor = new WorkAsyncInterceptorWarpper(interceptor);
            ProxyGenerator generator = new ProxyGenerator();
            var worker = generator.CreateInterfaceProxyWithTarget(target, wrapperInterceptor);
            worker.Start();
            worker.Excute();
            worker.ExcuteAsync();
            worker.ExcuteAsync(1);
            worker.Stop();
        }
        static void WorkProxyGenerationHook()
        {
            Console.WriteLine("WorkProxyGenerationHook");
            IWorker target = new Worker();
            var interceptor = new WorkAsyncInterceptor();
            var wrapperInterceptor = new WorkAsyncInterceptorWarpper(interceptor);
            var options = new ProxyGenerationOptions(new WorkProxyGenerationHook());
            ProxyGenerator generator = new ProxyGenerator();
            var worker = generator.CreateInterfaceProxyWithTarget(target, options, wrapperInterceptor);
            worker.Start();
            worker.Excute();
            worker.ExcuteAsync();
            worker.ExcuteAsync(1);
            worker.NonHook();
            worker.Stop();
        }

        static void IAsyncInterceptor()
        {
            Console.WriteLine("IAsyncInterceptor");
            IWorker target = new Worker();
            var interceptor = new WorkAsyncInterceptor();
            ProxyGenerator generator = new ProxyGenerator();
            var worker = generator.CreateInterfaceProxyWithTarget(target, interceptor);
            worker.Start();
            worker.Excute();
            worker.ExcuteAsync();
            worker.ExcuteAsync(1);           
            worker.Stop();
        }
    }
}
